﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.XtraReports.UI;
using DXApplicationTangche.Const;
using DXApplicationTangche.report;
using DXApplicationTangche.UC.下单MTO;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.客户CRM.Service;
using DXApplicationTangche.UC.成品出库单.service;
using DXApplicationTangche.UC.成衣进销存.service;
using DXApplicationTangche.UC.盘点.Service;
using DXApplicationTangche.UC.订单一览新.订单预览新.model;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.订单一览新.销售确认单;
using DXApplicationTangche.UC.货号管理;
using DXApplicationTangche.UC.钉钉;
using DXApplicationTangche.UC.销售记录.service;
using DXApplicationTangche.UC.门店出入库.Service;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DXApplicationTangche.UC.成衣进销存.service.成衣进销存Service;

namespace DXApplicationTangche.UC.门店出入库
{
    public partial class Form门店出入库 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public CustomerPDTO customerPDTO = new CustomerPDTO();
        public String getMobile { get; set; }

        //属性
        #region 当前店铺
        private String currentStoreId;
        private String currentStoreName;
        #endregion

        #region 调拨入库
        private StockInOutTitleDTO enterStockInOutTitleDTO = new StockInOutTitleDTO();
        private List<StockInOutDetailDTO> enterStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private List<StockInOutDetailDTO> enterShowStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private List<StockInOutDetailDTO> enterExtraStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private StockInOutDetailDTO enterStockInOutDetailDTO = new StockInOutDetailDTO();
        private String enterSessionId;
        private String enterOutStoreId;
        private String enterOutStoreName;
        private String enterMnemonic;
        private String enterName;
        private List<String> enterInBarcode = new List<String>();//已入库成衣条码
        #endregion

        #region 其他入库
        private StockInOutDetailDTO otherStockInOutDetailDTO = new StockInOutDetailDTO();
        private List<StockInOutDetailDTO> otherStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        #endregion

        #region 调拨出库
        private String offInOutCode;//出库单号
        private StockInOutTitleDTO offStockInOutTitleDTO = new StockInOutTitleDTO();
        private List<StockInOutDetailDTO> offStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private StockInOutDetailDTO offStockInOutDetailDTO = new StockInOutDetailDTO();
        private String offSessionId;
        private String offInStoreId;
        private String offInStoreName;
        private String offMnemonic;
        private String offName;
        private String offType { get; set; }
        private Enum成衣进销存 enum成衣进销存 = Enum成衣进销存.未知;
        #endregion

        #region 销售出库
        private StockInOutTitleDTO saleStockInOutTitleDTO = new StockInOutTitleDTO();
        private List<StockInOutDetailDTO> saleStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private StockInOutDetailDTO saleStockInOutDetailDTO = new StockInOutDetailDTO();
        private String saleSessionId;
        private String saleMnemonic;
        private String saleName;
        #endregion

        #region 销售退货
        private String saleReturnMnemonic;
        private String saleReturnName;
        #endregion

        #region 销售返厂
        private String stockReturnInSessionId;
        private StockInOutTitleDTO returnInStockInOutTitleDTO = new StockInOutTitleDTO();
        private List<StockInOutDetailDTO> returnInStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private StockInOutDetailDTO returnInStockInOutDetailDTO = new StockInOutDetailDTO();
        private String stockReturnMnemonic;//返厂操作员工号
        private String stockReturnName;//返厂操作员名称
        #endregion

        #region 盘盈入库
        private String stockSurplusMnemonic;//盘盈入库操作员工号
        private String stockSurplusName;//返厂操作员名称
        private String surplusSessionId;
        private StockInOutTitleDTO surplusStockInOutTitleDTO = new StockInOutTitleDTO();
        private List<StockInOutDetailDTO> surplusStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        private StockInOutDetailDTO surplusStockInOutDetailDTO = new StockInOutDetailDTO();
        #endregion

        #region 查库存
        private List<ViewStockInDetailDTO> viewStockInDetailDTOs = new List<ViewStockInDetailDTO>();
        private 订单预览新Model model;
        #endregion

        #region 其他返厂
        private List<StockInOutDetailDTO> otherReturnInStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
        #endregion

        #region searchLookUp数据源
        private List<DTO销售表头> 销售表头DTOs = new List<DTO销售表头>();
        private List<SalesAssistantDTO> salesAssistantDTO = new List<SalesAssistantDTO>();
        #endregion

        //构造方法
        #region 构造方法
        public Form门店出入库()
        {
            InitializeComponent();
            this.config读取店铺();
            this.offSessionId = new FunctionHelper().Uuid;
            this.saleSessionId = new FunctionHelper().Uuid;
            this.surplusSessionId = new FunctionHelper().Uuid;
            this.stockReturnInSessionId = new FunctionHelper().Uuid;
            this.销售表头DTOs = 销售记录Service.get店铺();
            this.salesAssistantDTO = 销售确认Service.get销售员带助记码();
            //this.searchLookUpEdit调拨出库的入库店铺.Properties.DataSource = this.销售表头DTOs;
            this.searchLookUpEdit调拨出库销售员.Properties.DataSource = this.salesAssistantDTO;
            this.searchLookUpEdit销售出库销售员.Properties.DataSource = this.salesAssistantDTO;
            this.searchLookUpEdit销售退货销售员.Properties.DataSource = this.salesAssistantDTO;
            this.searchLookUpEdit调拨入库销售员新.Properties.DataSource = this.salesAssistantDTO;
            this.repositoryItemSearchLookUpEdit当前店铺.DataSource = this.销售表头DTOs;
            this.searchLookUpEdit销售返厂销售员.Properties.DataSource = this.salesAssistantDTO;
            this.gridView门店其他入库.BestFitColumns();
        }
        #endregion
        private void config读取店铺()
        {
            if (System.IO.File.Exists(@"xml\config店铺.xml"))
            {
                DTO销售表头 dTO店铺 = DealXML.XMLFlieToObject<DTO销售表头>(@"xml\config店铺.xml", Encoding.UTF8);
                this.currentStoreId = dTO店铺.id;
                this.currentStoreName = dTO店铺.name;
                this.repositoryItemSearchLookUpEdit当前店铺.NullText = this.currentStoreName;
                this.barEditItem当前店铺.Enabled = false;
            }
        }

        //searchLookUp事件
        #region 选择当前店铺
        private void repositoryItemSearchLookUpEdit当前店铺_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear当前店铺Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void repositoryItemSearchLookUpEdit当前店铺View_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            //String str = this.repositoryItemSearchLookUpEdit当前店铺View.GetFocusedRowCellValue("id").ToString();
            this.currentStoreId = this.销售表头DTOs[e.RowHandle].id;
            this.currentStoreName = this.销售表头DTOs[e.RowHandle].name;
            this.barEditItem当前店铺.EditValue = this.currentStoreName;
            if (!System.IO.File.Exists(@"xml\config店铺.xml"))
            {
                DealXML.ObjectToXMLFile(new DTO销售表头(this.currentStoreId, this.currentStoreName), @"xml\config店铺.xml", Encoding.UTF8);
                this.config读取店铺();
            }
        }
        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear当前店铺Btn_Click(object sender, EventArgs e)
        {
            //this.repositoryItemSearchLookUpEdit当前店铺.ToolTip = null;
            //repositoryItemSearchLookUpEdit当前店铺.EditValue = null;
        }
        private void repositoryItemSearchLookUpEdit当前店铺_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.currentStoreName;
        }
        #endregion

        #region 选择调拨入库销售员新
        private void searchLookUpEdit调拨入库销售员新_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear调拨入库销售员新Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit调拨入库销售员新View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit调拨入库销售员新.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.enterMnemonic = this.searchLookUpEdit调拨入库销售员新.Properties.View.GetRowCellValue(rowHandle, "mnemonic").ToString();
                    this.enterName = this.searchLookUpEdit调拨入库销售员新.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear调拨入库销售员新Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit调拨入库销售员新.ToolTip = null;
            searchLookUpEdit调拨入库销售员新.EditValue = null;
        }
        private void searchLookUpEdit调拨入库销售员新_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.enterName;
        }
        #endregion

        #region 选择调拨出库的入库店铺

        #region searchLookUpEdit调拨出库的入库店铺 
        //private void searchLookUpEdit调拨出库店铺_Popup(object sender, EventArgs e)
        //{
        //    PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
        //    SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
        //    LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
        //    //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
        //    if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
        //    {
        //        //得到空的空间
        //        EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

        //        //得到取消按钮，重写点击事件
        //        Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
        //        LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
        //        clearBtn.Click += clear调拨出库店铺Btn_Click;

        //        //添加一个simplebutton控件(确认按钮)
        //        LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
        //        myLCI.TextVisible = false;
        //    }
        //}
        //private void searchLookUpEdit调拨出库店铺View_Click(object sender, EventArgs e)
        //{
        //    int[] a = this.searchLookUpEdit调拨出库的入库店铺.Properties.View.GetSelectedRows();
        //    if (a.Length > 0)
        //    {
        //        foreach (int rowHandle in a)
        //        {
        //            this.offInStoreId = this.searchLookUpEdit调拨出库的入库店铺.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
        //            this.offInStoreName = this.searchLookUpEdit调拨出库的入库店铺.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
        //        }
        //    }
        //}

        ///// <summary>
        ///// 清除按钮事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void clear调拨出库店铺Btn_Click(object sender, EventArgs e)
        //{
        //    this.searchLookUpEdit调拨出库的入库店铺.ToolTip = null;
        //    searchLookUpEdit调拨出库的入库店铺.EditValue = null;
        //}
        //private void searchLookUpEdit调拨出库店铺_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        //{
        //    if (null != e.Value)
        //        e.DisplayText = this.offInStoreName;
        //}
        #endregion

        #region menu
        private void 凯丹ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.offInStoreId = "1";
            this.offInStoreName = "凯丹";
            this.offType = 调拨出库类型Consts.平调;
            this.textBox调拨入库仓库.Text = this.offInStoreName;
        }

        private void 柏威年ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.offInStoreId = "2";
            this.offInStoreName = "柏威年";
            this.offType = 调拨出库类型Consts.平调;
            this.textBox调拨入库仓库.Text = this.offInStoreName;
        }

        private void 淘宝店ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.offInStoreId = "4";
            this.offInStoreName = "淘宝店";
            this.offType = 调拨出库类型Consts.平调;
            this.textBox调拨入库仓库.Text = this.offInStoreName;
        }

        private void 直播带货仓ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.offInStoreId = "7";
            this.offInStoreName = "直播带货仓";
            this.offType = 调拨出库类型Consts.平调;
            this.textBox调拨入库仓库.Text = this.offInStoreName;
        }

        private void LunaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.offInStoreId = "3";
            this.offInStoreName = "Luna";
            this.offType = 调拨出库类型Consts.Luna;
            this.textBox调拨入库仓库.Text = this.offInStoreName;
        }

        private void 销账ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.offInStoreId = "6";
            this.offInStoreName = "废品仓";
            this.offType = 调拨出库类型Consts.销账;
            this.textBox调拨入库仓库.Text = this.offInStoreName;
        }

        private void 返厂ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.offInStoreId = "8";
            this.offInStoreName = "退回冻结存货";
            this.offType = 调拨出库类型Consts.返厂;
            this.textBox调拨入库仓库.Text = this.offInStoreName;
        }

        private void 凯丹客户自提ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.offInStoreId = "11";
            this.offInStoreName = "凯丹客户自提";
            this.offType = 调拨出库类型Consts.自提;
            this.textBox调拨入库仓库.Text = this.offInStoreName;
        }

        private void 柏威年客户自提ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.offInStoreId = "12";
            this.offInStoreName = "柏威年客户自提";
            this.offType = 调拨出库类型Consts.自提;
            this.textBox调拨入库仓库.Text = this.offInStoreName;
        }
        #endregion
        #endregion

        #region 选择调拨出库销售员
        private void searchLookUpEdit调拨出库销售员_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear调拨出库销售员Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit调拨出库销售员View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit调拨出库销售员.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.offMnemonic = this.searchLookUpEdit调拨出库销售员.Properties.View.GetRowCellValue(rowHandle, "mnemonic").ToString();
                    this.offName = this.searchLookUpEdit调拨出库销售员.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear调拨出库销售员Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit调拨出库销售员.ToolTip = null;
            searchLookUpEdit调拨出库销售员.EditValue = null;
        }
        private void searchLookUpEdit调拨出库销售员_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.offName;
        }
        #endregion

        #region 选择销售出库销售员
        private void searchLookUpEdit销售出库销售员_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear销售出库销售员Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit销售出库销售员View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit销售出库销售员.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.saleMnemonic = this.searchLookUpEdit销售出库销售员.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.saleName = this.searchLookUpEdit销售出库销售员.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear销售出库销售员Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit销售出库销售员.ToolTip = null;
            searchLookUpEdit销售出库销售员.EditValue = null;
        }
        private void searchLookUpEdit销售出库销售员_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.saleName;
        }
        #endregion

        #region 选择销售退货销售员
        private void searchLookUpEdit销售退货销售员_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear销售退货销售员Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit销售退货销售员View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit销售退货销售员.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.saleReturnMnemonic = this.searchLookUpEdit销售退货销售员.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.saleReturnName = this.searchLookUpEdit销售退货销售员.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear销售退货销售员Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit销售退货销售员.ToolTip = null;
            searchLookUpEdit销售退货销售员.EditValue = null;
        }
        private void searchLookUpEdit销售退货销售员_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.saleReturnName;
        }
        #endregion

        #region 选择销售返厂销售员
        private void searchLookUpEdit销售返厂销售员_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear销售返厂销售员Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit销售返厂销售员View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit销售返厂销售员.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.stockReturnMnemonic = this.searchLookUpEdit销售返厂销售员.Properties.View.GetRowCellValue(rowHandle, "mnemonic").ToString();
                    this.stockReturnName = this.searchLookUpEdit销售返厂销售员.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear销售返厂销售员Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit销售返厂销售员.ToolTip = null;
            searchLookUpEdit销售返厂销售员.EditValue = null;
        }
        private void searchLookUpEdit销售返厂销售员_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.stockReturnName;
        }
        #endregion

        #region 选择盘盈入库销售员
        private void searchLookUpEdit盘盈入库销售员_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear盘盈入库销售员Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit盘盈入库销售员View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit盘盈入库销售员.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.stockSurplusMnemonic = this.searchLookUpEdit盘盈入库销售员.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.stockSurplusName = this.searchLookUpEdit盘盈入库销售员.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear盘盈入库销售员Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit盘盈入库销售员.ToolTip = null;
            searchLookUpEdit盘盈入库销售员.EditValue = null;
        }
        private void searchLookUpEdit盘盈入库销售员_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.stockSurplusName;
        }
        #endregion

        //控件事件
        #region 其他入库事件
        private void textBox其他入库扫描成衣码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.currentStoreId))
                {
                    MessageBox.Show("请选择当前店铺");
                    return;
                }
                if (!String.IsNullOrWhiteSpace(this.textBox其他入库扫描成衣码.Text))
                {
                    try
                    {
                        //  其他入库
                        this.otherStockInOutDetailDTO = 出入库Service.otherStockIn(this.textBox其他入库扫描成衣码.Text.Trim(), this.currentStoreId);
                        try { this.otherStockInOutDetailDTO.styleSizeCd = this.otherStockInOutDetailDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                        if (!String.IsNullOrEmpty(this.otherStockInOutDetailDTO.barcode))
                        {
                            if (this.otherStockInOutDetailDTOs.Count > 0)
                            {
                                if (!this.otherStockInOutDetailDTOs.Exists(dto => dto.barcode.Equals(this.otherStockInOutDetailDTO.barcode)))
                                {
                                    this.otherStockInOutDetailDTOs.Add(this.otherStockInOutDetailDTO);
                                }
                            }
                            else
                            {
                                this.otherStockInOutDetailDTOs.Add(this.otherStockInOutDetailDTO);
                            }
                        }
                        this.gridControl门店其他入库.DataSource = this.otherStockInOutDetailDTOs;
                        this.gridView门店其他入库.RefreshData();
                        this.gridView门店其他入库.BestFitColumns();
                        this.textBox其他入库扫描成衣码.Clear();
                    }
                    catch (Exception ex)
                    {
                        this.otherStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("请扫码");
                }
            }
        }

        #endregion

        #region 调拨出库事件
        private void textBox调拨出库扫描_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.currentStoreId))
                {
                    MessageBox.Show("请先选择当前店铺");
                    return;
                }
                if (String.IsNullOrEmpty(this.offInStoreId))
                {
                    MessageBox.Show("请先选择调拨入库店铺");
                    return;
                }
                if (String.IsNullOrEmpty(this.offMnemonic))
                {
                    MessageBox.Show("请选择操作人员");
                    return;
                }
                if (!String.IsNullOrWhiteSpace(this.textBox调拨出库扫描.Text))
                {
                    try
                    {
                        // 成衣库存类型判断
                        if (this.checkOrderType(this.textBox调拨出库扫描.Text.Trim()))
                        {
                            //扫描成衣码出库
                            //this.offStockInOutDetailDTO =
                            StockOutDetailLimitMessageReslt plainMessageResult =
                                成品出库单Service.preStockInOutDetailWithCustomerInfoByBarcodeLimit(
                                    this.offSessionId,
                                    this.textBox调拨出库扫描.Text.Trim(),
                                    0,
                                    this.enum成衣进销存.Equals(Enum成衣进销存.MTO库存) ? StockInOutTypeConst.MTODelivery : StockInOutTypeConst.ShopDeliveryOut,
                                    this.offInStoreId
                                    );
                            FunctionHelper.JsonSerialization(plainMessageResult.stockInOutDetailDTO);
                            if (!"200".Equals(plainMessageResult.errorCode))
                            {
                                //  最新出入库记录为出库，不能重复出库
                                StockInOutDetailDTO stockInOutDetailDTO = plainMessageResult.stockInOutDetailDTO;
                                MessageBox.Show(stockInOutDetailDTO.toString());
                                this.textBox调拨出库扫描.Clear();
                                return;
                            }
                            else
                            {
                                this.offStockInOutDetailDTO = plainMessageResult.stockInOutDetailDTO;
                                try { this.offStockInOutDetailDTO.styleSizeCd = this.offStockInOutDetailDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                                if (!String.IsNullOrEmpty(this.offStockInOutDetailDTO.barcode))
                                {
                                    if (this.offStockInOutDetailDTOs.Count > 0)
                                    {
                                        if (!this.offStockInOutDetailDTOs.Exists(dto => dto.barcode.Equals(this.offStockInOutDetailDTO.barcode)))
                                        {
                                            this.offStockInOutDetailDTOs.Add(this.offStockInOutDetailDTO);
                                        }
                                    }
                                    else
                                    {
                                        this.offStockInOutDetailDTOs.Add(this.offStockInOutDetailDTO);
                                    }
                                }
                                this.gridControl门店调拨出库.DataSource = this.offStockInOutDetailDTOs;
                                this.gridView门店调拨出库.RefreshData();
                                this.gridView门店调拨出库.BestFitColumns();
                            }
                        }
                        else
                        {
                            MessageBox.Show("订单类型与其它几件不同，请另外打印调拨单");
                            this.textBox调拨出库扫描.Clear();
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    this.textBox调拨出库扫描.Clear();
                }
                else
                {
                    MessageBox.Show("请扫码");
                }
            }
        }
        /// <summary>
        /// 判断成衣销售类型    this.textBox扫描.Text.Trim()
        /// </summary>
        /// <returns></returns>
        private Boolean checkOrderType(String barcode)
        {
            Boolean checkIsMTO = 成衣进销存Service.checkIsMTO(barcode);
            if (this.enum成衣进销存.Equals(Enum成衣进销存.未知))
            {
                this.enum成衣进销存 = checkIsMTO ? Enum成衣进销存.MTO库存 : Enum成衣进销存.MTS库存;
                return true;
            }
            else
            {
                if (this.enum成衣进销存.Equals((checkIsMTO ? Enum成衣进销存.MTO库存 : Enum成衣进销存.MTS库存)))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        private void simpleButton调拨出库确认_Click(object sender, EventArgs e)
        {
            if (this.offStockInOutDetailDTOs.Count > 0)
            {
                try
                {
                    this.splashScreenManager.ShowWaitForm();
                    this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中....");     // 标题
                    this.splashScreenManager.SetWaitFormDescription("正在初始化.....");     // 信息
                    this.offStockInOutTitleDTO =
                        成品出库单Service.getSaveStockInOutDetail(
                            this.offSessionId,
                            0,
                            this.enum成衣进销存.Equals(Enum成衣进销存.MTO库存) ? StockInOutTypeConst.MTODelivery : StockInOutTypeConst.ShopDeliveryOut,
                            this.offInStoreId,
                            this.offInStoreName,
                            this.currentStoreId,
                            this.currentStoreName,
                            this.offMnemonic);
                    //  加备注
                    if (!String.IsNullOrEmpty(this.richTextBox备注.Text))
                    {
                        this.offStockInOutTitleDTO = 成品出库单Service.saveStockInOutTitleRemarks(this.offSessionId, this.richTextBox备注.Text.Trim());
                    }
                    出库单导出DTO 出库单导出DTO = new 出库单导出DTO(this.offStockInOutTitleDTO, this.offStockInOutDetailDTOs);
                    String str = FunctionHelper.JsonSerialization(出库单导出DTO);
                    //List<出库单导出DTO> 出库单导出DTOs = new List<出库单导出DTO>();
                    //出库单导出DTOs.Add(出库单导出DTO);
                    XtraReport出库单 report = new XtraReport出库单(出库单导出DTO);
                    if (this.enum成衣进销存.Equals(Enum成衣进销存.MTO库存))
                    {
                        report.MTO库存();
                    }
                    else if (this.enum成衣进销存.Equals(Enum成衣进销存.MTS库存))
                    {
                        report.MTS库存();
                    }
                    try
                    {
                        //  平调、Luna、返厂有钉钉流程
                        if (调拨出库类型Consts.平调.Equals(this.offType) 
                            || 调拨出库类型Consts.Luna.Equals(this.offType) 
                            || 调拨出库类型Consts.返厂.Equals(this.offType)
                            || 调拨出库类型Consts.销账.Equals(this.offType)
                            )
                        {
                            //  上传pdf:
                            string saveDirectoryPath = @"pdf\";
                            //  1.先本地生成文件
                            report.ExportToPdf(saveDirectoryPath + this.offStockInOutTitleDTO.inOutCode + @".pdf");
                            //  2.上传本地文件
                            String re = 成品出库单Service.uploadStockInOutFileByCode(this.offStockInOutTitleDTO.inOutCode, saveDirectoryPath + this.offStockInOutTitleDTO.inOutCode + @".pdf");
                            //  3.删除本地文件
                            File.Delete(saveDirectoryPath + re);
                            //  钉钉
                            钉钉Service.start(new ProcessStartDTO(this.offType, this.offStockInOutTitleDTO, re).build("13940826881", 124740491));
                        }
                    }
                    catch(Exception ex) {
                        MessageBox.Show("服务连接失败，钉钉发送失败，请联系李金键");
                    }
                    ReportPrintTool tool = new ReportPrintTool(report);
                    tool.ShowPreview();
                    this.splashScreenManager.CloseWaitForm();
                    this.enum成衣进销存 = Enum成衣进销存.未知;
                    this.offStockInOutDetailDTOs.Clear();
                    this.offSessionId = new FunctionHelper().Uuid;
                    this.offStockInOutDetailDTO = new StockInOutDetailDTO();
                    this.gridControl门店调拨出库.DataSource = this.offStockInOutDetailDTOs;
                    this.gridView门店调拨出库.RefreshData();
                    this.gridView门店调拨出库.BestFitColumns();
                    //this.userControl出入库明细一览.refreshGrid();
                    MessageBox.Show("出库成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("无确认数据");
            }
        }

        private void 调拨出库取消ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.offSessionId = new FunctionHelper().Uuid;
            this.offStockInOutDetailDTOs.Clear();
            this.offStockInOutDetailDTO = new StockInOutDetailDTO();
            this.gridControl门店调拨出库.DataSource = this.offStockInOutDetailDTOs;
            this.gridView门店调拨出库.RefreshData();
            this.gridView门店调拨出库.BestFitColumns();
        }
        #endregion

        #region 销售出库事件
        private void textBox销售出库扫描_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.currentStoreId))
                {
                    MessageBox.Show("请先选择当前店铺");
                    return;
                }
                if (String.IsNullOrEmpty(this.saleMnemonic))
                {
                    MessageBox.Show("请选择操作人员");
                    return;
                }
                if (!String.IsNullOrWhiteSpace(this.textBox销售出库扫描.Text.Trim()))
                {
                    try
                    {
                        //  MTO订单的销售出库，客户信息直接带出来
                        if (成衣进销存Service.checkIsMTO(this.textBox销售出库扫描.Text.Trim()))
                        {
                            //barcode查询orderCommitDTO
                            OrderCommitDTO返回 orderCommitDTO =
                                订单预览新Service.getOrderCommitDTOByBarcode(this.textBox销售出库扫描.Text.Trim());
                            this.textBox客户姓名.Text = orderCommitDTO.customerName;
                            this.textBox客户电话.Text = orderCommitDTO.mobile;
                            this.memoEdit客户地址.Text = orderCommitDTO.addressProvince + orderCommitDTO.addressCity + orderCommitDTO.addressDistrict + orderCommitDTO.addressDetail;
                        }
                        this.saleStockInOutDetailDTO =
                            成品出库单Service.preStockInOutDetailWithCustomerInfoByBarcode(
                            this.saleSessionId,
                            this.textBox销售出库扫描.Text.Trim(),
                            0,
                            StockInOutTypeConst.ShopSaleOut,
                            this.currentStoreId
                            );
                        try { this.saleStockInOutDetailDTO.styleSizeCd = this.saleStockInOutDetailDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                        if (!String.IsNullOrEmpty(this.saleStockInOutDetailDTO.barcode))
                        {
                            if (this.saleStockInOutDetailDTOs.Count > 0)
                            {
                                if (!this.saleStockInOutDetailDTOs.Exists(dto => dto.barcode.Equals(this.saleStockInOutDetailDTO.barcode)))
                                {
                                    this.saleStockInOutDetailDTOs.Add(this.saleStockInOutDetailDTO);
                                }
                            }
                            else
                            {
                                this.saleStockInOutDetailDTOs.Add(this.saleStockInOutDetailDTO);
                            }
                        }
                        this.gridControl门店销售出库.DataSource = this.saleStockInOutDetailDTOs;
                        this.gridView门店销售出库.RefreshData();
                        this.gridView门店销售出库.BestFitColumns();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    this.textBox销售出库扫描.Clear();
                }
            }
        }

        private void simpleButton销售出库确认_Click(object sender, EventArgs e)
        {
            if (this.saleStockInOutDetailDTOs.Count > 0)
            {
                try
                {
                    //  现货销售出库
                    //this.saleStockInOutTitleDTO =
                    //    出入库Service.saveStockSaleOut(
                    //        this.saleSessionId,
                    //        this.currentStoreId,
                    //        this.currentStoreName,
                    //        this.saleMnemonic,
                    //        this.textBox客户姓名.Text.Trim(),
                    //        this.textBox客户性别.Text.Trim(),
                    //        this.textBox客户电话.Text.Trim(),
                    //        this.memoEdit客户地址.Text.Trim()
                    //        );
                    ////  提交销售确认
                    //try
                    //{
                    //    List<GarmentSaleConfirmDTO> garmentSaleConfirmDTOs = new List<GarmentSaleConfirmDTO>();
                    //    foreach (StockInOutDetailDTO item in this.saleStockInOutDetailDTOs)
                    //    {
                    //        garmentSaleConfirmDTOs.Add(
                    //            new GarmentSaleConfirmDTO(this.textBox客户电话.Text.Trim(), item.barcode, this.currentStoreId, this.saleMnemonic));
                    //    }
                    //    List<GarmentSaleConfirmDTO> 返回值garmentSaleConfirmDTOs = 销售确认Service.saveGarmentSaleConfirm(garmentSaleConfirmDTOs);
                    //    //FunctionHelper.JsonSerialization(返回值garmentSaleConfirmDTOs);
                    //}
                    //catch { }
                    //  
                    this.textBox客户姓名.Clear();
                    this.textBox客户性别.Clear();
                    this.textBox客户电话.Clear();
                    //this.memoEdit客户地址.Clear();
                    this.saleSessionId = new FunctionHelper().Uuid;
                    this.saleStockInOutDetailDTOs.Clear();
                    this.saleStockInOutDetailDTO = new StockInOutDetailDTO();
                    this.gridControl门店销售出库.DataSource = this.saleStockInOutDetailDTOs;
                    this.gridView门店销售出库.RefreshData();
                    this.gridView门店销售出库.BestFitColumns();
                    this.userControl出入库明细一览.refreshGrid();
                    MessageBox.Show("销售出库成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("无确认数据");
            }
        }

        private void 销售出库取消ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.textBox客户姓名.Clear();
            this.textBox客户性别.Clear();
            this.textBox客户电话.Clear();
            //this.memoEdit客户地址;
            this.saleSessionId = new FunctionHelper().Uuid;
            this.saleStockInOutDetailDTOs.Clear();
            this.saleStockInOutDetailDTO = new StockInOutDetailDTO();
            this.gridControl门店销售出库.DataSource = this.saleStockInOutDetailDTOs;
            this.gridView门店销售出库.RefreshData();
            this.gridView门店销售出库.BestFitColumns();
        }
        #endregion

        #region 销售退货事件
        private void textBox销售退货扫描_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.currentStoreId))
                {
                    MessageBox.Show("请选择当前店铺");
                    return;
                }
                if (String.IsNullOrEmpty(this.saleReturnMnemonic))
                {
                    MessageBox.Show("请选择操作人员");
                    return;
                }
                if (!String.IsNullOrWhiteSpace(this.textBox销售退货扫描.Text.Trim()))
                {
                    try
                    {
                        出入库Service.saveStockSaleReturnIn(
                            this.textBox销售退货扫描.Text.Trim(),
                            this.currentStoreId,
                            this.currentStoreName,
                            this.saleReturnMnemonic
                            );
                        this.textBox销售退货扫描.Clear();
                        this.userControl出入库明细一览.refreshGrid();
                        MessageBox.Show("入库成功");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
        #endregion

        #region 销售返厂事件
        private void textBox销售返厂扫描_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.currentStoreId))
                {
                    MessageBox.Show("请选择当前店铺");
                    return;
                }
                if (String.IsNullOrEmpty(this.stockReturnMnemonic))
                {
                    MessageBox.Show("请选择操作人员");
                    return;
                }
                if (String.IsNullOrEmpty(this.comboBox返厂类型.Text))
                {
                    MessageBox.Show("请选择返厂类型");
                    return;
                }
                if (!String.IsNullOrWhiteSpace(this.textBox销售返厂扫描.Text.Trim()))
                {
                    try
                    {
                        String inOutType = "";
                        switch (this.comboBox返厂类型.Text)
                        {
                            case "礼品":
                                inOutType = StockInOutTypeConst.ShopGiftReturnIn;
                                break;
                            case "残次品返厂":
                                inOutType = StockInOutTypeConst.DefectiveReturnIn;
                                break;
                            default:
                                inOutType = StockInOutTypeConst.DefectiveReturnIn;
                                break;
                        }
                        //扫描成衣码出库
                        this.returnInStockInOutDetailDTO =
                            出入库Service.preStockReturnInByBarcode(
                                this.stockReturnInSessionId,
                                this.textBox销售返厂扫描.Text.Trim(),
                                inOutType
                                );
                        try { this.returnInStockInOutDetailDTO.styleSizeCd = this.returnInStockInOutDetailDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                        if (!String.IsNullOrEmpty(this.returnInStockInOutDetailDTO.barcode))
                        {
                            if (this.returnInStockInOutDetailDTOs.Count > 0)
                            {
                                if (!this.returnInStockInOutDetailDTOs.Exists(dto => dto.barcode.Equals(this.returnInStockInOutDetailDTO.barcode)))
                                {
                                    this.returnInStockInOutDetailDTOs.Add(this.returnInStockInOutDetailDTO);
                                }
                            }
                            else
                            {
                                this.returnInStockInOutDetailDTOs.Add(this.returnInStockInOutDetailDTO);
                            }
                        }
                        this.textBox销售返厂扫描.Clear();
                        this.gridControl门店返厂出库.DataSource = this.returnInStockInOutDetailDTOs;
                        this.gridView门店返厂出库.RefreshData();
                        this.gridView门店返厂出库.BestFitColumns();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
        private void simpleButton返厂出库确认_Click(object sender, EventArgs e)
        {
            try
            {
                String inOutType = "";
                switch (this.comboBox返厂类型.Text)
                {
                    case "礼品":
                        inOutType = StockInOutTypeConst.ShopGiftReturnIn;
                        break;
                    case "残次品返厂":
                        inOutType = StockInOutTypeConst.DefectiveReturnIn;
                        break;
                    default:
                        inOutType = StockInOutTypeConst.DefectiveReturnIn;
                        break;
                }
                this.returnInStockInOutTitleDTO =
                    出入库Service.saveStockReturnIn(
                        this.stockReturnInSessionId,
                        this.currentStoreId,
                        this.currentStoreName,
                        this.stockReturnMnemonic,
                        inOutType,
                        this.richTextBox销售返厂备注.Text.Trim()
                        );
                出库单导出DTO 出库单导出DTO = new 出库单导出DTO(this.returnInStockInOutTitleDTO, this.returnInStockInOutDetailDTOs);
                String str = FunctionHelper.JsonSerialization(出库单导出DTO);
                //List<出库单导出DTO> 出库单导出DTOs = new List<出库单导出DTO>();
                //出库单导出DTOs.Add(出库单导出DTO);
                XtraReport出库单 report = new XtraReport出库单(出库单导出DTO);
                //report.DataSource = 出库单导出DTOs;
                ReportPrintTool tool = new ReportPrintTool(report);
                tool.ShowPreview();
                this.richTextBox销售返厂备注.Clear();
                this.stockReturnInSessionId = new FunctionHelper().Uuid;
                this.returnInStockInOutDetailDTOs.Clear();
                this.returnInStockInOutDetailDTO = new StockInOutDetailDTO();
                this.gridControl门店返厂出库.DataSource = this.returnInStockInOutDetailDTOs;
                this.gridView门店返厂出库.RefreshData();
                this.gridView门店返厂出库.BestFitColumns();
                this.userControl出入库明细一览.refreshGrid();
                MessageBox.Show("出库成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region Ribbon事件
        private void barButtonItem样衣登记一览_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new Form货号管理().ShowDialog();
        }

        private void barButtonItem成衣库存在途一览Excel导出_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();
                this.splashScreenManager.ShowWaitForm();
                this.splashScreenManager.SetWaitFormCaption("请稍后,正在保存中....");     // 标题
                this.splashScreenManager.SetWaitFormDescription("正在初始化.....");     // 信息
                出入库Service.SaveDataToExcelFile(
                    this.userControl出入库明细一览.getDataStockInOutDetailDTOs()
                    );
                this.splashScreenManager.CloseWaitForm();
                stopWatch.Stop();
                System.Diagnostics.Debug.WriteLine("====================库存Excel导出时间" + stopWatch.ElapsedMilliseconds + " ms=======================");
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region 调拨入库新事件
        private void textBox调拨入库扫描新_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.enterMnemonic))
                {
                    MessageBox.Show("请选择操作人员");
                    return;
                }
                if (!String.IsNullOrWhiteSpace(this.textBox调拨入库扫描新.Text))
                {
                    String code = this.textBox调拨入库扫描新.Text;
                    this.ClearAll();
                    this.textBox调拨入库扫描新.Text = code;
                    this.offInOutCode = this.textBox调拨入库扫描新.Text.Trim();
                    try
                    {
                        this.enterStockInOutDetailDTOs = 出入库Service.getStockOutToInHold(
                            this.offInOutCode
                            );
                        this.enterStockInOutTitleDTO = 出入库Service.getStockOutToInTitle(
                            this.offInOutCode
                            );
                        this.currentStoreId = enterStockInOutTitleDTO.InStoreId;
                        this.currentStoreName = enterStockInOutTitleDTO.InStoreName;
                        this.barEditItem当前店铺.EditValue = this.currentStoreName;
                        //扫描调拨单条码后显示调拨单明细
                        //this.gridControl门店调拨入库新.DataSource = this.enterStockInOutDetailDTOs;
                        this.ActiveControl = this.textBox调拨入库扫描成衣条码;
                    }
                    catch (Exception ex)
                    {
                        this.enterStockInOutDetailDTOs = new List<StockInOutDetailDTO>();
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("请扫码");
                }
            }
        }

        private void gridView门店调拨入库新_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            int hand = e.RowHandle;//行号
            try
            {
                if (!this.enterInBarcode.Exists(
                    barcode =>
                    barcode
                    .Equals(
                        FunctionHelper
                        .GetgridViewValue(
                            this.gridView门店调拨入库新,
                            hand,
                            "barcode"
                            )
                        )
                    )
                    )
                {
                    e.Appearance.BackColor = Color.Linen;//行背景颜色
                }
            }
            catch
            {
            }
        }

        private void textBox调拨入库扫描成衣条码_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.enterStockInOutDetailDTOs.Count < 1)
            {
                MessageBox.Show("请扫描出库单号");
                return;
            }
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrEmpty(this.textBox调拨入库扫描成衣条码.Text))
                {
                    if (this.enterShowStockInOutDetailDTOs.Exists(DTO => DTO.barcode.Equals(this.textBox调拨入库扫描成衣条码.Text.Trim())))
                    {
                        this.textBox调拨入库扫描成衣条码.Clear();
                        return;
                    }
                    if (this.enterStockInOutDetailDTOs.Exists(DTO => DTO.barcode.Equals(this.textBox调拨入库扫描成衣条码.Text.Trim())))
                    {
                        foreach (StockInOutDetailDTO dTO in this.enterStockInOutDetailDTOs)
                        {
                            if (dTO.barcode.Equals(this.textBox调拨入库扫描成衣条码.Text.Trim()))
                            {
                                this.enterShowStockInOutDetailDTOs.Add(dTO);
                                break;
                            }
                        }
                        this.gridControl门店调拨入库新.DataSource = this.enterShowStockInOutDetailDTOs;
                        this.gridView门店调拨入库新.RefreshData();
                        this.gridView门店调拨入库新.BestFitColumns();
                        this.textBox调拨入库扫描成衣条码.Clear();
                    }
                    else
                    {
                        StockInOutDetailDTO stock = 出入库Service.getStockInOutDetailDaoWhenExtraIn(this.textBox调拨入库扫描成衣条码.Text.Trim(), this.currentStoreId);
                        try
                        {
                            if (!String.IsNullOrEmpty(stock.barcode))
                            {
                                if (!this.enterExtraStockInOutDetailDTOs.Exists(DTO => DTO.barcode.Equals(stock.barcode)))
                                {
                                    this.enterExtraStockInOutDetailDTOs.Add(stock);
                                }
                            }
                        }
                        catch { }
                        //  批量人入库需要把这个注释
                        this.gridControl门店调拨入库新多出的.DataSource = this.enterExtraStockInOutDetailDTOs;
                        this.gridView门店调拨入库新多出的.RefreshData();
                        this.gridView门店调拨入库新多出的.BestFitColumns();
                        this.textBox调拨入库扫描成衣条码.Clear();
                    }
                    this.enterInBarcode.Add(this.textBox调拨入库扫描成衣条码.Text.Trim());
                }
                else
                {

                }
            }
        }

        private void simpleButton调拨入库确认新_Click(object sender, EventArgs e)
        {
            Form调拨入库确认 form调拨入库确认 = new Form调拨入库确认(
                this.offInOutCode,
                this.currentStoreId,
                this.enterMnemonic,
                this.enterStockInOutDetailDTOs,
                this.enterShowStockInOutDetailDTOs,
                this.enterExtraStockInOutDetailDTOs
                );
            form调拨入库确认.clearAllEvents += this.ClearAll;
            form调拨入库确认.ShowDialog();

            // 批量入库
            //if(this.enterStockInOutDetailDTOs.Count == 0)
            //{
            //    MessageBox.Show("请扫描调拨单");
            //    return;
            //}
            //try
            //{
            //    出入库Service.saveStockOutToInConfirm(
            //        this.offInOutCode,
            //        this.currentStoreId,
            //        this.enterMnemonic,
            //        this.enterStockInOutDetailDTOs
            //        );
            //    MessageBox.Show("入库成功");
            //    this.enterStockInOutDetailDTOs.Clear();
            //    this.textBox调拨入库扫描新.Clear();
            //    this.gridControl门店调拨入库新.DataSource = null;
            //    //this.clearAllEvents();
            //    //this.Close();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("入库失败" + ex.Message);
            //}
        }

        private void ClearAll()
        {
            this.enterStockInOutDetailDTOs.Clear();
            this.enterShowStockInOutDetailDTOs.Clear();
            this.gridControl门店调拨入库新.DataSource = this.enterShowStockInOutDetailDTOs;
            this.gridView门店调拨入库新.RefreshData();
            this.gridView门店调拨入库新.BestFitColumns();
            this.enterExtraStockInOutDetailDTOs.Clear();
            this.gridControl门店调拨入库新多出的.DataSource = this.enterExtraStockInOutDetailDTOs;
            this.gridView门店调拨入库新多出的.RefreshData();
            this.gridView门店调拨入库新多出的.BestFitColumns();
            this.enterInBarcode.Clear();
            this.textBox调拨入库扫描新.Text = "";
            this.offInOutCode = "";
        }
        #endregion

        #region 盘盈入库事件
        private void textBox盘盈入库扫描成衣条码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.currentStoreId))
                {
                    MessageBox.Show("请先选择当前店铺");
                    return;
                }
                if (String.IsNullOrEmpty(this.stockSurplusMnemonic))
                {
                    MessageBox.Show("请选择操作人员");
                    return;
                }
                if (!String.IsNullOrWhiteSpace(this.textBox盘盈入库扫描成衣条码.Text))
                {
                    try
                    {
                        //扫描成衣码出库
                        this.surplusStockInOutDetailDTO =
                            成品出库单Service.preStockInOutDetailWithCustomerInfoByBarcode(
                                this.surplusSessionId,
                                this.textBox盘盈入库扫描成衣条码.Text.Trim(),
                                1,
                                StockInOutTypeConst.StockInventoryIn,
                                this.currentStoreId
                                );
                        try { this.surplusStockInOutDetailDTO.styleSizeCd = this.surplusStockInOutDetailDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                        if (!String.IsNullOrEmpty(this.surplusStockInOutDetailDTO.barcode))
                        {
                            if (this.surplusStockInOutDetailDTOs.Count > 0)
                            {
                                if (!this.surplusStockInOutDetailDTOs.Exists(dto => dto.barcode.Equals(this.surplusStockInOutDetailDTO.barcode)))
                                {
                                    this.surplusStockInOutDetailDTOs.Add(this.surplusStockInOutDetailDTO);
                                }
                            }
                            else
                            {
                                this.surplusStockInOutDetailDTOs.Add(this.surplusStockInOutDetailDTO);
                            }
                        }
                        this.gridControl盘盈入库.DataSource = this.surplusStockInOutDetailDTOs;
                        this.gridView盘盈入库.RefreshData();
                        this.gridView盘盈入库.BestFitColumns();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    this.textBox盘盈入库扫描成衣条码.Clear();
                }
                else
                {
                    MessageBox.Show("请扫码");
                }
            }
        }

        private void simpleButton盘盈入库确认_Click(object sender, EventArgs e)
        {
            if (this.surplusStockInOutDetailDTOs.Count > 0)
            {
                try
                {
                    this.surplusStockInOutTitleDTO =
                        成品出库单Service.getSaveStockInOutDetail(
                            this.surplusSessionId,
                            1,
                            StockInOutTypeConst.StockInventoryIn,
                            this.currentStoreId,
                            this.currentStoreName,
                            this.currentStoreId,
                            this.currentStoreName,
                            this.stockSurplusMnemonic
                            );
                    this.surplusSessionId = new FunctionHelper().Uuid;
                    this.surplusStockInOutDetailDTOs.Clear();
                    this.surplusStockInOutDetailDTO = new StockInOutDetailDTO();
                    this.gridControl盘盈入库.DataSource = this.surplusStockInOutDetailDTOs;
                    this.gridView盘盈入库.RefreshData();
                    this.gridView盘盈入库.BestFitColumns();
                    this.userControl出入库明细一览.refreshGrid();
                    MessageBox.Show("盘盈入库成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("无确认数据");
            }
        }

        private void 盘盈入库取消ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.surplusSessionId = new FunctionHelper().Uuid;
            this.surplusStockInOutDetailDTOs.Clear();
            this.surplusStockInOutDetailDTO = new StockInOutDetailDTO();
            this.gridControl盘盈入库.DataSource = this.surplusStockInOutDetailDTOs;
            this.gridView盘盈入库.RefreshData();
            this.gridView盘盈入库.BestFitColumns();
        }

        #endregion

        //private void textBox扫成衣码查库存_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyValue == 13)
        //    {
        //        if (String.IsNullOrEmpty(this.textBox扫成衣码查库存.Text.Trim()))
        //        {
        //            MessageBox.Show("请扫码");
        //            return;
        //        }
        //        try
        //        {
        //            this.model = new 订单预览新Model(this.textBox扫成衣码查库存.Text.Trim(), Enum订单查询.按照水洗标查询订单);
        //        }
        //        catch (Exception)
        //        {
        //            MessageBox.Show("请正确扫描成衣条码");
        //            return;
        //        }
        //        //款式
        //        this.gridControl款式.DataSource = this.model.款式DTOs;
        //        //设计点
        //        this.gridControl设计点.DataSource = this.model.设计点DTOs;
        //        //面料
        //        this.gridControl面料.DataSource = this.model.面料DTOs;
        //        this.viewStockInDetailDTOs = 出入库Service.getViewStockInDetailByBarcode(this.textBox扫成衣码查库存.Text.Trim());
        //        this.pivotGridControl同款在库一览.DataSource = this.viewStockInDetailDTOs;
        //        this.pivotGridControl同款在库一览.RefreshData();
        //    }
        //}

        private void simpleButton更多客户_Click(object sender, EventArgs e)
        {
            new FormMTO客户一览(this.customerPDTO).ShowDialog();
            this.getTextByMobile(this.customerPDTO.mobile);
        }
        private void getTextByMobile(String getMobile)
        {
            this.getMobile = getMobile;
            this.customerPDTO = CRM客户Service.getCustomerPByMobile(getMobile);
            this.textBox客户姓名.Text = this.customerPDTO.customerName;
            this.textBox客户电话.Text = this.customerPDTO.mobile;
            this.memoEdit客户地址.Text =
                this.customerPDTO.addressProvince + "\n"
                + this.customerPDTO.addressCity + "\n"
                + this.customerPDTO.addressDistrict + "\n"
                + this.customerPDTO.addressDetail;
            this.textBox客户姓名.Enabled = false;
            this.textBox客户电话.Enabled = false;
            
        }

        private void simpleButton添加客户_Click(object sender, EventArgs e)
        {
            new FormMTO添加客户地址().ShowDialog();
        }

        private void simpleButton特价款下单_Click(object sender, EventArgs e)
        {
            this.textBox销售出库扫描.Text = "20210329-87y";
            try
            {
                //  MTO订单的销售出库，客户信息直接带出来
                if (成衣进销存Service.checkIsMTO(this.textBox销售出库扫描.Text.Trim()))
                {
                    //barcode查询orderCommitDTO
                    OrderCommitDTO返回 orderCommitDTO =
                        订单预览新Service.getOrderCommitDTOByBarcode(this.textBox销售出库扫描.Text.Trim());
                    this.textBox客户姓名.Text = orderCommitDTO.customerName;
                    this.textBox客户电话.Text = orderCommitDTO.mobile;
                    this.memoEdit客户地址.Text = orderCommitDTO.addressProvince + orderCommitDTO.addressCity + orderCommitDTO.addressDistrict + orderCommitDTO.addressDetail;
                }
                this.saleStockInOutDetailDTO =
                    成品出库单Service.preStockInOutDetailWithCustomerInfoByBarcode(
                    this.saleSessionId,
                    this.textBox销售出库扫描.Text.Trim(),
                    0,
                    StockInOutTypeConst.ShopSaleOut,
                    this.currentStoreId
                    );
                try { this.saleStockInOutDetailDTO.styleSizeCd = this.saleStockInOutDetailDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                if (!String.IsNullOrEmpty(this.saleStockInOutDetailDTO.barcode))
                {
                    if (this.saleStockInOutDetailDTOs.Count > 0)
                    {
                        if (!this.saleStockInOutDetailDTOs.Exists(dto => dto.barcode.Equals(this.saleStockInOutDetailDTO.barcode)))
                        {
                            this.saleStockInOutDetailDTOs.Add(this.saleStockInOutDetailDTO);
                        }
                    }
                    else
                    {
                        this.saleStockInOutDetailDTOs.Add(this.saleStockInOutDetailDTO);
                    }
                }
                this.gridControl门店销售出库.DataSource = this.saleStockInOutDetailDTOs;
                this.gridView门店销售出库.RefreshData();
                this.gridView门店销售出库.BestFitColumns();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.textBox销售出库扫描.Clear();
        }

        private void barButtonItem重选店铺_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (System.IO.File.Exists(@"xml\config店铺.xml"))
            {
                File.Delete(@"xml\config店铺.xml");
                this.barEditItem当前店铺.Enabled = true;
                this.currentStoreId = "";
                this.currentStoreName = "";
                this.repositoryItemSearchLookUpEdit当前店铺.NullText = "";
            }
        }

        private void textBox其他返厂扫码_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.textBox其他返厂扫码.Text.Trim()))
                {
                    return;
                }
                try
                {
                    StockInOutDetailDTO stockInOutDetailDTO = 出入库Service.preOtherReturnInByBarcode(this.textBox其他返厂扫码.Text.Trim());
                    if (!String.IsNullOrEmpty(stockInOutDetailDTO.barcode))
                    {
                        otherReturnInStockInOutDetailDTOs.Add(stockInOutDetailDTO);
                    }
                }
                catch { }
                this.gridControl其他返厂.DataSource = this.otherReturnInStockInOutDetailDTOs;
                this.gridView其他返厂.RefreshData();
                this.gridView其他返厂.BestFitColumns();
                this.textBox其他返厂扫码.Clear();
            }
        }

        private void gridView门店调拨入库新_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumnstyleSizeCd2"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                else
                {
                    e.DisplayText = "样衣无尺寸定义";
                }
            }

        }
    }
}